import * as types from '../mutation-types'
import {
  queryIndex,
  sign
} from '@/service/weixin'

const unSignedStyle = 'fill:#c4c4c4'
const originPoint = {
  '5': {
    points: '188,100 138,150 188,200',
    style: 'fill:#6ae9d8;'
  },
  '4': {
    points: '138,150 138,250 188,200',
    style: 'fill:#5dc382;'
  },
  '2': {
    points: '163,75 138,100 163,125',
    style: 'fill:#f588d9;'
  },
  '6': {
    points: '188,200 163,225 188,250',
    style: 'fill:#57adea;'
  },
  '1': {
    points: '128,260 198,260 163,225',
    style: 'fill:#f4d330;'
  },
  '0': {
    points: '138,100 163,125 138,150 113,125',
    style: 'fill:#ff9d4a;'
  },
  '3': {
    points: '188,50 188,100 163,125 163,75',
    style: 'fill:#fb6068;'
  }
}

const state = {
  score: '0',
  signAllStatus: '0',
  signTodayStatus: '0',
  signList: [],
  signedList: [],
  sign6State: {},
  imgData: '',
  imgSrc: '',
  polygonData: [],
  originPoint,
  imgDataList: [],
  signRule: ''
}

const getters = {
  signedList: (state) => {
    if (state.signList.length === 7) {
      return state.signList.slice(0, 6)
    }
    return [{
      signStatus: '0'
    },
    {
      signStatus: '0'
    },
    {
      signStatus: '0'
    },
    {
      signStatus: '0'
    },
    {
      signStatus: '0'
    },
    {
      signStatus: '0'
    }
    ]
  },
  sign6State: (state) => {
    if (state.signList.length === 7) {
      return state.signList[6]
    }
    return {
      signStatus: '0'
    }
  },
  polygonData: (state) => {
    var polygonData = state.polygonData
    if (!polygonData.length) {
      for (var key in Object.keys(originPoint)) {
        let data = originPoint[key]
        polygonData.push({
          index: key,
          points: data.points,
          style: unSignedStyle,
          movable: false
        })
      }
    }
    return polygonData
  },
  imgDataList: state => state.imgDataList,
  signRule: state => state.signRule
}

const actions = {
  async [types.QUERY_INDEX] ({
    commit
  }, param) {
    const res = await queryIndex(param)
    commit(types.QUERY_INDEX, { res })
  },
  async [types.SIGN] ({
    commit
  }, param) {
    const res = await sign(param)
    commit(types.QUERY_INDEX, { res })
    return res
  }
}

const asyncPolygonData = (state) => {
  var polygonData = state.polygonData
  var len = polygonData.length
  if (len) {
    for (var i = 0; i < len; i += 1) {
      let data = polygonData[i]
      var signStatus = state.signList[i].signStatus
      var style = unSignedStyle
      var movable = false
      if (signStatus === '1') {
        style = originPoint[data.index].style
        movable = true
        originPoint[data.index].points = data.points
      }
      polygonData[i].style = style
      polygonData[i].movable = movable
    }
  }
  state.polygonData = polygonData
}

const mutations = {
  [types.QUERY_INDEX] (state, { res }) {
    var data = res.data
    if (data) {
      state.score = data.score || '0'
      state.signAllStatus = data.signAllStatus || '0'
      state.signTodayStatus = data.signTodayStatus || '0'
      state.signList = data.signList
      state.imgSrc = data.imgSrc
      state.signRule = data.signRule
      var imgData = data.imgData
      if (imgData) {
        imgData = JSON.parse(JSON.parse(imgData))
        state.polygonData = imgData
      }
      var imgDataList = data.imgDataList
      if (imgDataList) {
        imgDataList.forEach(item => {
          item.imgPath = process.env.IMG_FILE_PATH + item.imgPath
        })
        state.imgDataList = imgDataList
      }
      asyncPolygonData(state)
    }
  },
  [types.SIGN] (state, {res}) {
    var data = res.data
    if (data) {
      state.score = data.score
    }
  }
}

export default {
  state,
  getters,
  mutations,
  actions
}
